# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.44.1
pkgrel=3
pkgdesc="Graph Visualization Software (mingw-w64)"
arch=('any')
url='https://www.graphviz.org/'
license=('EPL')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-devil"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-gtkglext"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libglade"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-gts"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libtool")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-ocaml"
             #"${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python2"
             #"${MINGW_PACKAGE_PREFIX}-ruby"
             #"${MINGW_PACKAGE_PREFIX}-tcl"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "git")
options=('strip')
install=${_realname}-${CARCH}.install
source=(#${_realname}-${pkgver}::git+https://gitlab.com/graphviz/graphviz.git#tag=stable_release_${pkgver}
        https://www2.graphviz.org/Packages/stable/portable_source/graphviz-${pkgver}.tar.gz
        001-msvc-pragma.patch.patch
        002-no-undefined-and-def-files.patch
        003-fix-dependency-detect.patch
        004-exports.patch
        005-plugin-exports.patch
        006-missing-libs.patch
        007-broken-freeglut.patch
        008-fix-dll-names-in-def.patch
        009-fix-warnings.patch
        010-no-extern-time.patch
        011-strcasecmp-mingw.patch
        012-cast-error.patch)
sha256sums=('8e1b34763254935243ccdb83c6ce108f531876d7a5dfd443f255e6418b8ea313'
            'a64e23a55481ff36641466ef80607bcdc1bbf20f992804620f4a58b5b8c04f8d'
            'd1e7e4dc1b55463123e3ed8f999745bf06078766ee24071be8866ac75fe1309e'
            'bd1d89894af4b89d14cfa028238aa87f0a6044c43f988f39a0c8512ab07285a2'
            'b5ce65be7a0f8012aecdf15229e3db8012ddc78152fa849a54ffe87ca6bfe119'
            '35ee3d44abcb6d30a10ea5a797b214b19e929a0555297a8dce35e4842c059b15'
            '8fe551e8fc8c7f048c8c497f983e3bec60ac9489dec307508160199a5162ab3c'
            '6f066e2d08d296355da0e735429ade1a459940225288a70939f547c36562f982'
            '26a80a0071a0821de8019463788e966047551a93451386f5cf209c77cefe65e9'
            '0ac5c4b11297ed751f32ab6e165d75f27d7ec30efc3c26c23b73fbce2b097179'
            '01387eb1c9544b9e8aeb66a6ec23777c5bb3e2770b1672d2596b2b3d550319fd'
            'f9a4aa81bf2f918cc608dc49852c57782b610c2e56fea7e43a53a7c5bd655e90'
            'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-msvc-pragma.patch.patch
  patch -p1 -i ${srcdir}/002-no-undefined-and-def-files.patch
  patch -p1 -i ${srcdir}/003-fix-dependency-detect.patch
  patch -p1 -i ${srcdir}/004-exports.patch
  patch -p1 -i ${srcdir}/005-plugin-exports.patch
  patch -p1 -i ${srcdir}/006-missing-libs.patch
  patch -p1 -i ${srcdir}/007-broken-freeglut.patch
  patch -p1 -i ${srcdir}/008-fix-dll-names-in-def.patch
  patch -p1 -i ${srcdir}/009-fix-warnings.patch
  patch -p1 -i ${srcdir}/010-no-extern-time.patch
  patch -p1 -i ${srcdir}/011-strcasecmp-mingw.patch
  patch -p1 -i ${srcdir}/012-cast-error.patch

  ./autogen.sh NOCONFIG
}

build() {
  # declare -a extra_config
  # if check_option "debug" "n"; then
    # extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  # else
    # extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  # fi

  # [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  # mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  # MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  # ${MINGW_PREFIX}/bin/cmake.exe \
    # -G"MSYS Makefiles" \
    # -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    # ${extra_config} \
    # ../${_realname}-${pkgver}

  # make

  local gd_incdir=$(pkg-config --variable=includedir gdlib)
  CFLAGS+=" -Wno-sign-conversion -Wno-sign-compare -Wno-conversion -fcommon"
  CXXFLAGS+=" -Wno-sign-conversion -Wno-sign-compare -Wno-conversion -fcommon"
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-guile=no \
    --enable-perl=no \
    --enable-php=no \
    --enable-ocaml=no \
    --enable-tcl=no \
    --enable-ruby=no \
    --enable-python=no \
    --enable-lua=no \
    --enable-r=no \
    --enable-sharp=no \
    --enable-swig=no \
    --enable-java=no \
    --with-webp=yes \
    --without-qt \
    --with-gdincludedir=${gd_incdir} \
    --enable-shared \
    --disable-static

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install
}
